

from machine import Pin, I2C,PWM
import ssd1306
import urequests
import json
import network,time

def do_connect(screen):
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        screen.text('connecting to ',0,0,1)
        screen.text('CMCC-PPVi network...',0,8,1)
        screen.show()
        wlan.connect('CMCC-PPVi', '37jp7j5b')
        while not wlan.isconnected():
            pass
    screen.fill(0)
    screen.text('network config:',0,0,1)
    screen.text( wlan.ifconfig()[0],0,8,1)
    screen.show()

def init_display():
    i2c = I2C(sda=Pin(16), scl=Pin(5))
    display = ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3c)    
    display.contrast(255)
    return display
def get_data():
    res=urequests.get('https://api.vvhan.com/api/en?type=sj').text
    res_dic=json.loads(res)
    res_data=res_dic['data']['en']
    print(res_data)
    return res_data
def split_string(strings,length):
    result=[]
    for i in range(0,len(strings),length):
        result.append(strings[i:i+length])
    return result

def direction(rate):
    # 舵机输出引脚
    pwm2=PWM(Pin(2,Pin.OUT))
    # 设置pwm频率
    pwm2.freq(50)
    # 设置转动角度
    c=rate
    ts = int((c / 180 * 2 + 0.5) / 20 * 1023)
    pwm2.duty(ts)
# ---控制运动的函数,d4>d5后退，d5>d4前进，差距越大越快---
def run(d4,d5):
    freq=50
    pwm4=PWM(Pin(4,Pin.OUT))
    pwm4.freq(freq)
    pwm5=PWM(Pin(0,Pin.OUT))
    pwm5.freq(freq)
    
    pwm4.duty(d4)
    pwm5.duty(d5)
# screen=init_display()
# for i in range(10):
#     angle=90
#     direction(angle)
#     screen.text(str(angle),0,0,1)
#     screen.show()
#     time.sleep(2)
#     angle=0
#     direction(angle)
#     screen.text(str(angle),0,0,1)
#     screen.show()
#     time.sleep(2)
run(0,0)
# 90度垂直向下，65度右满，115右满
direction(65)





